<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleExportData()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.preId }}
        </a>
      </template>
      <template #uploadFile="{ record }">
        <!-- disabled -->
        <BasicUpload
          v-model:value="record.dataMap"
          :bizKey="record.id"
          :bizType="'yf_sh_file'"
          disabled
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBusShYfShRecordList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { yfShRecordDelete, yfShRecordListData } from '/@/api/bus/sh/yfShRecord';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { BasicUpload } from '/@/components/Upload';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { m8ViewRouteTreeData } from '/@/api/bas/route/m8ViewRoute';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  const { hasPermission } = usePermission();
  const { ctxPath } = useGlobSetting();

  const { t } = useI18n('bus.sh.yfShRecord');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('收运记录管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('收运时间'),
        field: 'createDate',
        component: 'RangePicker',
      },
      {
        label: t('医疗机构'),
        field: 'hospName',
        component: 'Input',
      },
      {
        label: t('线路'),
        field: 'm8ViewHosp.routeCode',
        fieldLabel: 'm8ViewHosp.croute',
        component: 'TreeSelect',
        componentProps: {
          api: m8ViewRouteTreeData,
          params: { isShowCode: false },
          // canSelectParent: false,
          allowClear: true,
        },
        ifShow: hasPermission(['data:route:show']),
      },
      {
        label: t('线路顺序'),
        field: 'm8ViewHosp.routeSort',
        component: 'Input',
        ifShow: hasPermission(['data:route:show']),
      },
      {
        label: t('驾驶员'),
        field: 'driverName',
        component: 'Input',
      },
      {
        label: t('收运状态'),
        field: 'shStatus',
        component: 'Select',
        componentProps: {
          dictType: 'yf_sh_status',
          allowClear: true,
        },
      },
      {
        label: t('来源'),
        field: 'csource',
        component: 'Select',
        componentProps: {
          dictType: 'yf_sh_source',
          allowClear: true,
        },
      },
    ],
    fieldMapToTime: [['createDate', ['createDate_gte', 'createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    // {
    //   title: t('医院编号'),
    //   dataIndex: 'hospCode',
    //   key: 'a.hosp_code',
    //   sorter: true,
    //   width: 100,
    //   align: 'left',
    //   fixed: 'left',
    // },
    {
      title: t('医疗机构'),
      dataIndex: 'hospName',
      key: 'a.hosp_name',
      sorter: true,
      width: 280,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('线路'),
      dataIndex: 'm8ViewHosp.croute',
      key: 'hosp.croute',
      sorter: true,
      width: 100,
      align: 'left',
      ifShow: hasPermission(['data:route:show']),
    },
    {
      title: t('线路顺序'),
      dataIndex: 'm8ViewHosp.routeSort',
      key: 'hosp.route_sort',
      sorter: true,
      width: 100,
      align: 'left',
      ifShow: hasPermission(['data:route:show']),
    },
    {
      title: t('收运人'),
      dataIndex: 'creator.userName',
      key: 'a.create_by',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('驾驶员'),
      dataIndex: 'driverName',
      key: 'a.driver_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('押运员'),
      dataIndex: 'driver2Name',
      key: 'a.driver2_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车牌号'),
      dataIndex: 'carNo',
      key: 'a.car_no',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('收运时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('确认收运时间'),
      dataIndex: 'overDate',
      key: 'a.over_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    // {
    //   title: t('确认人'),
    //   dataIndex: 'overBy',
    //   key: 'a.over_by',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    {
      title: t('感染性箱数'),
      dataIndex: 'qtyGr',
      key: 'a.qty_gr',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('损伤性箱数'),
      dataIndex: 'qtyPs',
      key: 'a.qty_ps',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('总箱数'),
      dataIndex: 'qtySum',
      key: 'a.qty_sum',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('感染性重量'),
      dataIndex: 'weightGr',
      key: 'a.weight_gr',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('损伤性重量'),
      dataIndex: 'weightPs',
      key: 'a.weight_ps',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('总重量'),
      dataIndex: 'weightSum',
      key: 'a.weight_sum',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('来源'),
      dataIndex: 'csource',
      key: 'a.csource',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'yf_sh_source',
    },
    {
      title: t('收运图片'),
      dataIndex: 'uploadFile',
      width: 120,
      align: 'center',
      slot: 'uploadFile',
      fixed: 'right',
    },
    {
      title: t('收运状态'),
      dataIndex: 'shStatus',
      key: 'a.sh_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'yf_sh_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 120,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑收运记录'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bus:sh:yfShRecord:modify',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除收运记录'),
        popConfirm: {
          title: t('是否确认删除收运记录'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'bus:sh:yfShRecord:delete',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: yfShRecordListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    ellipsis: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await yfShRecordDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 报表方式导出
  async function handleExportData() {
    let searchForm = getForm().getFieldsValue();
    const params = new URLSearchParams(searchForm).toString();
    downloadByUrl({
      url: ctxPath + `/ureport/excel?_u=file:收运记录.ureport.xml&_n=收运记录&${params}`,
    });
  }

  function handleSuccess() {
    reload();
  }
</script>
